package S17;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;

public class RomanToInteger {
	public static String[] romans = {  "M", "CM","D", "CD", "C","XC","L","XL","X","IX","V","IV","I"};
	public static int[] 	integers= {1000, 900, 500, 400, 100, 90,  50, 40,  10, 9,   5,   4,  1}; 
    
	public int romanToInt(String s) {
		
        int res  = 0 ;
        if(s == null)
        	return 0 ;
        
        for(int i=0; i<s.length(); i++) {
        	StringBuffer sbt = new StringBuffer();
        	sbt.append(s.charAt(i));
        	
        	if(i + 1 < s.length()) {
        		sbt.append(s.charAt(i+1));
        		int index = getIndex(sbt.toString());
        		
        		if(index >= 0) {
        			res += integers[index];
        			i++;
        			continue;
        		}
        		
        		sbt.deleteCharAt(1);
        		
        		
        	}
        	res += integers[getIndex(sbt.toString())];
        	
        }
        
        return res;
    }
	
	
	
	public static int getIndex(String s) {
		for(int i=0; i<romans.length; i++) {
			if(romans[i].equals(s))
				return i;
		}
		return -1;
	}
	
	public static void main(String[] args) {
		System.out.println(new RomanToInteger().romanToInt("MCMXCIV"));
	}
}
